package cn.uikit.demo.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import sivin.uikit.utils.dpf


class KtTargetProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    private val outSpace = 2f.dpf
    private val outlineWidth = 3f.dpf
    private val ringWidth = 12f.dpf

    private var progress1 = 0f
    private var progress2 = 0f
    private var progress3 = 0f

    private var progress1Color = Color.parseColor("#FF5D2F")
    private var progress2Color = Color.parseColor("#437FEC")
    private var progress3Color = Color.parseColor("#67D298")

    private var bgColor = Color.parseColor("#F4F5F7") // 默认圆的颜色
    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = bgColor
        style = Paint.Style.FILL // 填充样式
    }

    private val outLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE // 默认圆的颜色
        style = Paint.Style.STROKE // 填充样式
        strokeWidth = outlineWidth
    }

    private val progressWith = ringWidth
    private val paintIndicator by lazy {
        Paint().apply {
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.BUTT
            color = Color.RED
            strokeWidth = progressWith
        }
    }

    private var progressRectF: RectF = RectF(0.0f, 0.0f, 0.0f, 0.0f)


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val size = width.coerceAtMost(height)
        val radius = size / 2f
        val centerX = width / 2f
        val centerY = height / 2f
        bgPaint.color = bgColor
        canvas.drawCircle(centerX, centerY, radius, bgPaint)
        canvas.drawCircle(centerX, centerY, radius - outlineWidth / 2 - outSpace, outLinePaint)

        bgPaint.color = Color.WHITE
        canvas.drawCircle(centerX, centerY, radius - outlineWidth - outSpace - ringWidth, bgPaint)

        val offset = outSpace + outlineWidth + progressWith / 2

        progressRectF.set(
            offset,
            offset,
            size - offset,
            size - offset
        )
        paintIndicator.color = progress1Color
        canvas.drawArc(
            progressRectF,
            -90f, progress1,
            false,
            paintIndicator
        )

        if (progress2 > 1) {
            paintIndicator.color = progress2Color
            canvas.drawArc(
                progressRectF,
                progress1 - 90f, progress2,
                false,
                paintIndicator
            )
        }

        if (progress3 > 1) {
            paintIndicator.color = progress3Color
            canvas.drawArc(
                progressRectF,
                progress1 + progress2 - 90f, progress3,
                false,
                paintIndicator
            )
        }
    }


    fun setDegrees(degrees: Int) {
        progress1 = degrees.coerceAtLeast(0)
            .coerceAtMost(360).toFloat()
    }

    fun setDegrees2(degrees: Int) {
        progress2 = degrees.coerceAtLeast(0)
            .coerceAtMost(360).toFloat()
    }

    fun setDegrees3(degrees: Int) {
        progress3 = degrees.coerceAtLeast(0)
            .coerceAtMost(360).toFloat()
    }

    fun setDegreesColor(color: Int) {
        progress1Color = color
    }

    fun setDegrees2Color(color: Int) {
        progress2Color = color
    }

    fun setDegrees3Color(color: Int) {
        progress3Color = color
    }

}